// pages/guide/item/index.js
const app = getApp()
const utils = require('../../../utils/util.js')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    navHeight: app.globalData.navHeight,
    headerHeight:40, // header title 高度
    isIphoneX: app.globalData.isIphoneX,
    header: '',
    guideList:[] ,
    pageIndex:1,
    rows:10,
    hasNoDatas:false,
    toCrmParams:'',
    isRefresh:false,
    boundButtonRect:{
      
    },
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBoundButtonRect()
    if(options && options.id) {
      this.setData({
        categoryId: options.id,
        header: decodeURIComponent(options.name)
      })
    }
    this.setupInit()
    this.getGuideData()
    this.setData({
      toCrmParams: JSON.stringify({ url: '/pages/guide/main/index' })
    })
  },

  // 最开始获取数据 1 刷新 ,2,加载更多,
  getGuideData:function(type = 1){
    const categoryId = this.data.categoryId
    const params = {
      page : this.data.pageIndex,
      rows : this.data.rows,
      onlyCountFlag: false,
      pageFlag: true,
      totalFlag: true
    }
    utils.$http(app.globalData.api.GetGuideListItem+categoryId, params, 'POST', false, "JSON").then(({success,data,msg})=>{
         if(success === 1){
            if(type === 1){
              this.setData({
                guideList: data.records,
                isRefresh:false
              })
              wx.stopPullDownRefresh();
            }
            else
            {
              if(data.records.length === 0){
                  this.setData({
                    hasNoDatas:true
                  })
              }else{
                const list = this.data.guideList.concat(data.records)
                this.setData({
                  guideList: list,
                  hasNoDatas: false
                })
              }
            }
        }
    }).catch(() => {
      this.setData({
        isRefresh: false
      })
    })
  },

  loadNewData:function(){
    this.data.pageIndex = 1
    this.data.isRefresh = true;
    this.getGuideData(1)
  },

  loadMoreData:function(){
    this.data.pageIndex = this.data.pageIndex + 1
    this.getGuideData(2)
  },

  setupInit: function () {
    utils.getFrameById('#header', (frame) => {
      // console.log('height:', frame.height)
      if (frame && frame.height) {
        this.setData({
          headerHeight: frame.height
        })
      }
    })
  },

// 取消收藏
addCollect(e){
  // console.log('e:',e)
  const item = e.currentTarget.dataset.item
  const openId =  wx.getStorageSync("openId") || ''

  const param = {
    openId: 'openId',
    programmeId: item.programmeId,
   }
   

  utils.$http(app.globalData.api.CollectOrCancel, param, "POST", false, "JSON").then(({ success})=>{
    if (success === 1)
    {
      this.getMyCollection()
    }
  })
},

  clickItem:function(e) {
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/guide/index/index?id=${encodeURIComponent(item.id)}`
    })
  },

  getBoundButtonRect:function(){
    let data = wx.getMenuButtonBoundingClientRect()
    const screenWidth =  app.globalData.screenWidth 
    let left = screenWidth - data.right
    let top = data.top
    let height = data.height
    let width = data.width
    this.setData(
      {
         boundButtonRect: {
           left,
           height,
           top,
           width
         }
      }
    )
  },

  goBack:function(){
    const currentPages = getCurrentPages()
    if (currentPages.length <= 1){
       wx.switchTab({
         url: '/pages/home/main/index',
       })
    }
    else
    {
      wx.navigateBack({

      })
    }
  
  },

  goHome:function(){
    wx.switchTab({
      url: '/pages/home/main/index'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadNewData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //  console.log('我来了......')
    //  this.loadMoreData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})